package com.luhuiguo.cms.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.GlobalConstants;
import com.luhuiguo.cms.client.GlobalMessages;
import com.luhuiguo.cms.client.GlobalResources;
import com.luhuiguo.cms.client.presenter.AttachmentProxy;
import com.luhuiguo.cms.client.presenter.AttachmentsPresenter;
import com.luhuiguo.cms.shared.model.AttachmentModel;

@Singleton
public class AttachmentsView extends ContentPanel implements AttachmentsPresenter.Display {

    @SuppressWarnings("unused")
    private GlobalConstants constants;

    private GlobalMessages messages;

    private GlobalResources resources;

    private AttachmentProxy proxy;

    private ListLoader<ListLoadResult<ModelData>> loader;

    private ListStore<BeanModel> store;

    private Grid<BeanModel> grid;

    private Button addButton;

    private Button deleteButton;

    private EditAttachmentView editAttachmentView;

    @Inject
    public AttachmentsView(GlobalConstants constants, GlobalMessages messages, GlobalResources resources,
            AttachmentProxy proxy, EditAttachmentView editAttachmentView) {
        this.constants = constants;
        this.messages = messages;
        this.resources = resources;
        this.proxy = proxy;

        this.editAttachmentView = editAttachmentView;
        init();

    }

    private void init() {

        loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, new BeanModelReader());
        // loader.setRemoteSort(true);

        store = new ListStore<BeanModel>(loader);

        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
        CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
        ColumnConfig col = sm.getColumn();
        columns.add(col);
        //
        // col = new ColumnConfig("id", messages.id(), 50);
        // columns.add(col);

        col = new ColumnConfig("filename", messages.filename(), 200);
        col.setRenderer(new GridCellRenderer<BeanModel>() {

            @Override
            public Object render(BeanModel model, String property, ColumnData config, int rowIndex, int colIndex,
                    ListStore<BeanModel> store, Grid<BeanModel> grid) {
                AttachmentModel a = model.getBean();
                String ret = "";
                if (AttachmentModel.TYPE_IMAGE == a.getType()) {
                    // ret =
                    // AbstractImagePrototype.create(resources.imageIcon()).getHTML();
                    ret = "<img src='/attachments/" + a.getId() + "/" + a.getFilename() + "' alt='" + a.getTitle()
                            + "' border='0' class='icon16' />";
                } else {
                    ret = AbstractImagePrototype.create(resources.attachIcon()).getHTML();
                }

                ret = ret + "<a href='/attachments/" + a.getId() + "/" + a.getFilename() + "' target='_blank'>"
                        + a.getFilename() + "</a>";

                return ret;
            }
        });

        columns.add(col);

        ColumnModel cm = new ColumnModel(columns);

        grid = new Grid<BeanModel>(store, cm);
        grid.setSelectionModel(sm);
        grid.setStripeRows(false);
        grid.addPlugin(sm);
        grid.setAutoExpandColumn("filename");
        grid.setLoadMask(true);

        ToolBar toolBar = new ToolBar();

        addButton = new Button(messages.addAttachment(), AbstractImagePrototype.create(resources.addIcon()));
        // addButton.setEnabled(true);
        deleteButton = new Button(messages.deleteAttachment(), AbstractImagePrototype.create(resources.deleteIcon()));
        deleteButton.setEnabled(false);

        toolBar.add(addButton);
        toolBar.add(new SeparatorToolItem());
        toolBar.add(deleteButton);

        setTopComponent(toolBar);

        setHeading(messages.attachments());
        setLayout(new FitLayout());
        add(grid);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void reloadData() {
        loader.load();

    }

    @Override
    public Button getAddButton() {
        return addButton;
    }

    @Override
    public Button getDeleteButton() {
        return deleteButton;
    }

    @Override
    public Grid<BeanModel> getGrid() {
        return grid;
    }

    @Override
    public void addWidget(Widget widget) {

    }

    @Override
    public void removeWidget(Widget widget) {

    }

    @Override
    public void showWidget(Widget widget) {

    }

    @Override
    public EditAttachmentView getEditAttachmentView() {
        return editAttachmentView;
    }

}
